package cn.stronglink.asset.module.assetmanage.instock.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.toolkit.IdWorker;

import cn.stronglink.asset.model.PresetDetail;
import cn.stronglink.asset.module.assetmanage.instock.mapper.PresetDetailMapper;
import cn.stronglink.asset.module.assetmanage.itasset.mapper.ItassetMapper;
import cn.stronglink.suite.core.util.WebUtil;

@Service
@Transactional(rollbackFor=Exception.class) 
public class PresetDetailService {

	@Autowired
	private PresetDetailMapper presetDetailMapper;
	@Autowired
	private ItassetMapper itassetMapper;

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<PresetDetail> queryDetailList(Long presetId) {
		return presetDetailMapper.queryDetailList(presetId);	
	}
	
	public void removePresetDetail(Long id){
		presetDetailMapper.removePresetDetail(id);	
	}

	public void insertPresetDetail(PresetDetail presetDetail) {
		if (presetDetail.getId() == null) {
			presetDetail.setId(IdWorker.getId());
			presetDetail.setStatus(1);
			presetDetail.setCreateBy(WebUtil.getCurrentUser());
			presetDetail.setCreateTime(new Date());
			presetDetailMapper.insertPresetDetail(presetDetail);		
		}else{
			presetDetail.setUpdateTime(new Date());
			presetDetail.setUpdateBy(WebUtil.getCurrentUser());
			presetDetailMapper.updatePresetDetail(presetDetail);
		}	
		
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public PresetDetail qryDetailById(Long id) {
		return presetDetailMapper.qryDetailById(id);
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<String> queryAllDetailList() {
		return  presetDetailMapper.queryAllDetailList();
	}

	public int createPresetDetailList(List<PresetDetail> presetDetailList) {
		return presetDetailMapper.createPresetDetailList(presetDetailList);
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Integer checkAssetNoNum(String assetNo,Long presetDetailId) {
		return presetDetailMapper.checkAssetNoNum(assetNo,presetDetailId);
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Boolean checkRfidNo(String rfidNo, Long assetId, Long presetDetailId) {
		Boolean flag =false;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rfidNo", rfidNo);
		map.put("assetId", assetId);
		int count =itassetMapper.qryByRfidNo(map);
		
		map.put("presetDetailId", presetDetailId);
		int rfidCount =presetDetailMapper.checkRfidNo(map);
		if(count>0||rfidCount>0){
			flag =true;
		}
		return flag;
	}

	public Boolean checkSn(String serialNo, Long presetDetailId) {
		Boolean flag =false;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("serialNo", serialNo);
		map.put("assetId", "");
		int count = itassetMapper.existsSerialNo(map);
		map.put("presetDetailId", presetDetailId);
		int snCount =presetDetailMapper.checkSn(map);
		if(count>0||snCount>0){
			flag =true;
		}
		return flag;
		
	}
	

	

}
